package com.jiangxiaofei.citycloud.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.activity.user.LoginAcitivity;
import com.jiangxiaofei.citycloud.adapter.MeSettingMenuRecyAdapter;
import com.jiangxiaofei.citycloud.bean.MenuBean;
import com.jiangxiaofei.citycloud.bean.UserInfoBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.event.MenuOnclickMessage;
import com.jiangxiaofei.citycloud.util.SharedPreferencesUtil;
import com.jiangxiaofei.citycloud.view.FullyGridLayoutManager;
import com.jiangxiaofei.citycloud.view.IOSAlertDialog;
import com.thinkcool.circletextimageview.CircleTextImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by JiangXFei on 2017/8/9.
 */

public class MeFragment extends Fragment implements View.OnClickListener {

    @InjectView(R.id.me_cimg_photo)
    CircleTextImageView meCimgPhoto;
    @InjectView(R.id.me_tv_user_name)
    TextView meTvUserName;
    @InjectView(R.id.me_recy_favorite_menu)
    RecyclerView meRecyMeMenu;
    @InjectView(R.id.me_recy_user_menu)
    RecyclerView meRecyUserMenu;
    private View meView;
    private Context mContext;
    private Activity mActivity;
    private List<MenuBean> menuBeanList;
    int[] menuImg = {
            R.drawable.ic_certification, R.drawable.ic_article,
            R.drawable.ic_car_info, R.drawable.ic_resume,
            R.drawable.ic_recruit, R.drawable.ic_favorite};
    int[] userSetImg = {R.drawable.ic_update_user_info,
            R.drawable.ic_update_pwd, R.drawable.ic_system_setting,
            R.drawable.ic_about_me};
    private MeSettingMenuRecyAdapter meSettingAdapter;
    private MeSettingMenuRecyAdapter userSetAdapter;
    private List<MenuBean> userMenuList;
    private UserInfoBean userInfo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        meView = inflater.inflate(R.layout.fragment_me_layout, null);
        mContext = getActivity();
        mActivity = getActivity();
        ButterKnife.inject(this, meView);
        initUserMenu();
        initFavoriteMenu();
        EventBus.getDefault().register(this);
        return meView;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        if (EmptyUtils.isNotEmpty(Constants.userInfo)) {
            userInfo = Constants.userInfo;
            meTvUserName.setText(userInfo.getName());
            meCimgPhoto.setOnClickListener(null);
        } else {
            meTvUserName.setText("请登陆");
            meCimgPhoto.setOnClickListener(this);
        }
    }

    private void initFavoriteMenu() {
        menuBeanList = new ArrayList<>();
        FullyGridLayoutManager layoutManager = new FullyGridLayoutManager(mContext, 4);
        meRecyMeMenu.setLayoutManager(layoutManager);
        meSettingAdapter = new MeSettingMenuRecyAdapter(menuBeanList, mContext, StringConstants.OTHERSET);
        meRecyMeMenu.setAdapter(meSettingAdapter);
        String[] menuStrings = mContext.getResources().getStringArray(R.array.me_favorite_menu);
        for (int i = 0; i < menuStrings.length; i++) {
            MenuBean bean = new MenuBean(menuStrings[i], menuImg[i]);
            menuBeanList.add(bean);
        }
        meSettingAdapter.notifyDataSetChanged();
    }

    private void initUserMenu() {
        userMenuList = new ArrayList<>();
        FullyGridLayoutManager layoutManager = new FullyGridLayoutManager(mContext, 4);
        meRecyUserMenu.setLayoutManager(layoutManager);
        userSetAdapter = new MeSettingMenuRecyAdapter(userMenuList, mContext, StringConstants.USERSET);
        meRecyUserMenu.setAdapter(userSetAdapter);
        String[] stringArray = mContext.getResources().getStringArray(R.array.me_menu);
        for (int i = 0; i < stringArray.length; i++) {
            MenuBean menuBean = new MenuBean(stringArray[i], userSetImg[i]);
            userMenuList.add(menuBean);
        }
        userSetAdapter.notifyDataSetChanged();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OnRecyItemOnclick(MenuOnclickMessage msg) {
        if (msg.getType().equals(StringConstants.USERSET)) {
            String name = userMenuList.get(msg.getPostion()).getName();
            if (name.equals("退出登陆")) {
                if (EmptyUtils.isNotEmpty(Constants.userInfo)) {
                    new IOSAlertDialog(mContext)
                            .builder()
                            .setTitle("提示")
                            .setMsg("是否退出登陆")
                            .setNegativeButton("取消", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {

                                }
                            })
                            .setPositiveButton("确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Constants.userInfo = null;
                                    SharedPreferencesUtil.setPrefBoolean(mContext, StringConstants.ISLOGIN, false);
                                    initData();
                                }
                            }).show();

                }
            }
        } else if (msg.getType().equals(StringConstants.OTHERSET)) {

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.me_cimg_photo:
                mActivity.startActivity(new Intent(mContext, LoginAcitivity.class));
                break;
            default:
                break;
        }
    }
}
